//
//  JSSStatusTableViewCell.swift
//  WeiBoItem
//
//  Created by jss on 16/4/21.
//  Copyright © 2016年 jss. All rights reserved.
//

import UIKit
import SnapKit

//头像的宽
let JSSStatusIconImageW :CGFloat = 40
//昵称的字体大小
let JSSStatusNameFont :CGFloat = 15
//创建来源的字体大小
let JSSStatusSourceFone:CGFloat = 14
//创建时间的字体大小
let JSSStatusTimeFone:CGFloat = 10
//cell里面的公用的间距
let JSSStatusMargin:CGFloat = 10
//正文字体大小和转发字体大小
let JSSStatusContentFont:CGFloat = 14
//底部视图的高度
let JSSStatusToolBarH:CGFloat = 35


class JSSStatusTableViewCell: UITableViewCell {
    
 

    
    
    //toolBar的顶部约束
    var toolBarTopContrant:Constraint?
    
    //U以前拿数据都是直接在Model里面拿现在又包裹了一层VIewModel(通过这个就可以拿到数据了)
//    var status: JSSStatusModel?
    var statusViewMdoel: JSSStatusViewModel? {
        
        didSet {
            
            OriginalView.statusViewModel = statusViewMdoel
            
//            RetweetView.statusViewModel = statusViewMdoel
            
            //取到toolar的top约束 让其失效
            toolBarTopContrant?.uninstall()
            
        //判断是否有转发微博 如果有 就设置转发微博数据
            if statusViewMdoel?.statusModel?.retweeted_status != nil {
                //设置数据
                RetweetView.statusViewModel = statusViewMdoel
                //显示显示微博
                RetweetView.hidden = false
                //更改约束 toolBar的top为转发微博的bottom
                ToolBarView.snp_makeConstraints(closure: { (make) -> Void in
                    
                    toolBarTopContrant = make.top.equalTo(RetweetView.snp_bottom).constraint
                })
                
            }else {
                
                // //没有转发微博
                
                //1隐藏转发微博
                RetweetView.hidden = true
                //2更新约束toolbar的top为original(原创)的底部
                
                ToolBarView.snp_makeConstraints(closure: { (make) -> Void in
                    
                    toolBarTopContrant = make.top.equalTo(OriginalView.snp_bottom).constraint
                })
            }
        }
    }
    
    
 
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //设置cell界面
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        //给cell设置颜色主要是为了测试 每个cell之间的相隔的间距(分割线)的实现(横条view)
           //self.backgroundColor = UIColor.redColor()
        
        
       //设置cell的被选择状态的样式
        selectionStyle = .None
        
        
        //往contentView上面设置颜色
        self.contentView.backgroundColor = UIColor(white: 237/255, alpha: 1)
        //把原发的View添加在cell的contentView上
        contentView.addSubview(OriginalView)
        contentView.addSubview(ToolBarView)
        contentView.addSubview(RetweetView)
        
        //约束JSSOriginalStatusView
        OriginalView.snp_makeConstraints { (make) -> Void in
            
            //make.top.leading.trailing.equalTo(contentView)
    
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(10)

           // make.height.equalTo(60)

        }
        
        //约束RetweetView(转发微博View)
        RetweetView.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.equalTo(OriginalView)
            make.top.equalTo(OriginalView.snp_bottom)
            //            make.bottom.equalTo(ToolBarView.snp_top)
            //高度根据内容来定
            //make.height.equalTo(50)
        }
        
         //约束ToolBarView(底部toolbar)
        ToolBarView.snp_makeConstraints { (make) -> Void in
            
//            make.top.equalTo(OriginalView.snp_bottom)
//            make.leading.trailing.equalTo(OriginalView)
//            make.height.equalTo(JSSStatusToolBarH)
           toolBarTopContrant = make.top.equalTo(RetweetView.snp_bottom).constraint
            make.leading.trailing.equalTo(OriginalView)
            make.height.equalTo(JSSStatusToolBarH)
        
        }
        
    
        
        //设置contentView约束
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.trailing.equalTo(self)
            make.leading.trailing.equalTo(self)
            
            make.bottom.equalTo(ToolBarView.snp_bottom)
//            make.top.equalTo(self).offset(-10)
        }
        
    }
    
    
    
    //懒加载原发View
    private lazy var OriginalView:JSSOriginalStatusView = JSSOriginalStatusView()
    
    //懒加载 底部viewtusView
    private lazy var ToolBarView:JSSToolBarStatusView = JSSToolBarStatusView()
    
    //懒加载 中间转发微博
    private lazy var RetweetView:JSSRetweetStatusView = JSSRetweetStatusView()

}
